let form, element, layer, toast;
let scene;
$("#captcha").attr("src", '/captcha?d=' + Math.random());
layui.use(['layer', 'form', 'element', 'toast'], function () {
    form = layui.form;
    element = layui.element;
    layer = layui.layer;
    toast = layui.toast;
    getShowQrcode();
});
function getShowQrcode() {
    $.get("/api/weChat/getShowQrcode", function (rep) {
        console.log(rep);
        $("#wxLogin").attr("src", rep.data.img);
        scene = rep.data.scene;
        getQRStatus();
    })
}
function getQRStatus() {
    var time = setInterval(function () {
        $.get("/api/weChat/getShowQrcode/" + scene, function (rep) {
            console.log(rep);
            var status = rep.data.status;
            if( - 1 == status) {
                clearTimeout(time);
                toast.error({message: "已超时", position: 'topCenter'});
                //getShowQrcode();
                layer.confirm('已超时,是否重新获取？', {
                    btn: ['是','还是'] //按钮
                }, function(){
                    getQRStatus();
                }, function(){
                    getQRStatus();
                });
            }
            if (1 == status) {
                toast.success({message: "已扫码，等待操作", position: 'topCenter'});
            }
            if (2 == status) {
                clearTimeout(time);
                toast.success({message: "登录成功", position: 'topCenter'});
                window.location.href = rep.data.url;
            }
        })
    }, 2000);
}

document.onkeydown = function (event) {
    var e = event || window.event;
    if (e && e.keyCode === 13) {
        $(".p-login-btn").click();
    }
};
